Core Servlet and JSP第二版第一册读书笔记
This file recorded notes during learning Core Servlet and JSP.
A sample web.xml
:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Servlet Testing</display-name> <description> This web application demonstrates the usage of Servlet/JSP (mainly Servlet). The contents are composed while reading Core Servlet and JSP Volume 2. </description> <!-- the following definition seems no effect... --> <icon> <small-icon>/small.png</small-icon> <large-icon>/large.gif</large-icon> </icon> <!-- the following definition seems no effect... --> <mime-mapping> <extension>png</extension> <mime-type>text/plain</mime-type> </mime-mapping> <context-param> <param-name>host</param-name> <param-value>nj-kelvin-hu</param-value> </context-param> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/exception.jsp</location> </error-page> <listener> <listener-class>ini.kelvin.servlet.ContextListener</listener-class> </listener> <listener> <listener-class>ini.kelvin.servlet.SessionListener</listener-class> </listener> <filter> <filter-name>print</filter-name> <filter-class>ini.kelvin.servlet.PrintFilter</filter-class> <init-param> <param-name>admin</param-name> <param-value>Kelvin Hu</param-value> </init-param> </filter> <filter-mapping> <filter-name>print</filter-name> <servlet-name>Hello</servlet-name> </filter-mapping> <filter-mapping> <filter-name>print</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>ini.kelvin.servlet.HelloWorldServlet</servlet-class> <init-param> <param-name>name</param-name> <param-value>Kelvin Hu</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <session-config> <session-timeout>1</session-timeout> </session-config> </web-app>
The value of url-pattern
must start with /
or *.
.
Matching Overlapping Patterns:
- Exact matches handled first: http://.../foo/bar will be handled by
/foo/bar
, not/foo/*
, also,/foo/bar.html
will win over*.html
, if the URL is http://.../foo/bar.html. - Directory mapping are preferred over extension mappings: the URL http://.../foo/bar.html will be handled by
/foo/*
, not the pattern*.html
. - For overlapping directory mappings, the longest path is preferred: http://.../foo/bar/baz.html will be handled by
/foo/bar/*
, not/foo/*
.
Parameters defined in <init-param>
could be read by the method getInitParameter()
of ServletConfig
.
<load-on-startup>
tells server the sequence that the servlet should be loaded into memory, lower number goes first, if two servlets have the same number, the load sequence is decided by server. Negative number cannot guarantee the servlet loading at startup.
When defining the <location>
of <error-page>
, be careful of the leading slash, so <location>/404.jsp</location>
will work, but if the slash /
removed, 404.jsp
will be not found.
HTTP/1.1 common request headers
Accept
: specify the MIME types that the browser can handleAccept-Charset
: the character charsetAccept-Encoding
: the encoding type, the most commonly used is "gzip, deflate"Accept-Language
: the language the browser exceptedAuthorization
: used to identify the browser when visiting some pages with password protectionConnection
: to indicate if the browser can handle continuous HTTP connection, continuous connection can transfer several files(html, images, etc) in one socket, saved the cost of establishing multiple connectionsContent-Length
: only used forPOST
, specify the length ofPOST
requestCookie
: return cookies back to http server, these cookies are formerly sent to browser by http serverHost
: the host name and port number in the requested URLIf-Modified-Since
: indicate the requested page is wanted only if the page has been modified after the specified date, otherwise the http server will send 304(Not Modified) instead, servlet should not handle this header directly, but implementgetLastModified()
method, let the server to handle the date comparisonIf-Unmodified-Since
: the opposite one ofIf-Modified-Since
Referer
: the URL which referenced current pageUser-Agent
: to identify the client browser which sent the request
HTTP/1.1 common response headers
Allow
: specify request methods the server supportedCache-Control
: tell client how to cache the document, can be set with the following values:public
: the document can be cachedprivate
: the document can only be cached in private cache for single userno-cache
: do not cache the document (old browsers usePragma
, so this header should also be set tono-cache
for old browsers)no-store
: do not cache, even do not store in temp folder on local diskmust-revalidate
: client must communicate with server to validate the document everytime when using itproxy-revalidate
: similar to previous one, only adapted to shared cachemax-age=XXX
: the document will be invalid after XXX seconds, it is the replacement of headerExpires
, and it has higher priority when both of them exists-max-age=XXX
: the shared cache should make the document invalid after XXX seconds
Connection
: used for continuouse connection, the value "close" tells the browser not to use continuous connection, default is continuous connectionContent-Disposition
: make the browser to ask user to store the response with the specified name on disk, as below:Content-Disposition: attachment; filename=some-file-name
Content-Encoding
: the encoding used during response transmissionContent-Language
: the language response usedContent-Length
: the byte count of response, only used for continuous connectionContent-Type
: the MIME of response, example:Content-Type: text/html; charset=utf-8
Expires
: seeCache-Control
andPragma
Last-Modified
: the last modified time of the documentLocation
: required when status code is between 300 and 399, used to notify the browser where the document is stored, the browser will automatically connect to the new address and get the new documentPragma
: seeCache-Control
andExpires
Refresh
: tell browser to send request for the newest page after the interval, example use:Refresh: 5; URL=http://host/path
Retry-After
: used with status code 503, tell the client to retry after the intervalSet-Cookie
: specify a cookie, every cookie needs a stand alone headerWWW-Authenticate
: used with 401, tell browser the authentication type and domain needed in request headerAuthorization